<?php

/**
 * Accesses the tgp_tokens table in the thegridpaper database
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_Tokens extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_tokens';
    protected $_primary = "token_id";
    protected $_dependentTables = array('AccountTokens');

    /**
     * Inserts a new token
     * @param string $value The value of the token
     * @return int: The id of the newly inserted token
     */
    public function insertToken($value)
    {
        $data = array('value' => $value);
        return $this->insert($data);
    }

    /**
     * Gets a token by its primary key
     * @param int $token_id The id of the token
     * @return NULL|array: An array containing the token's row if found, NULL otherwise
     */
    public function getById($token_id)
    {
        $token_id = (int)$token_id;
        $row = $this->find($token_id);
        if (!$row || $row->count() == 0)
        {
            return null;
        }
        return $row->current()->toArray();
    }

    /**
     * Gets a token by its value
     * @param string $value The value of the token
     * @return NULL|array: An array containing the token's row if found, NULL otherwise
     */
    public function getByValue($value)
    {
        $select = $this->select()->where('value = ?', $value);
        $row = $this->fetchRow($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }
}

